package jbpm;

import java.util.List;

import junit.framework.TestCase;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;

public class ProcessInstanceTest extends TestCase {

	//----发布流程定义  
    //第一步：启动流程引擎  
    ProcessEngine engine;  
    protected void setUp(){  
        engine.getRepositoryService().createDeployment()  
        .addResourceFromClasspath("hello.jpdl.xml").deploy();  
    }  
    public ProcessInstanceTest(){  
    	engine=Configuration.getProcessEngine();  
    }  
      
    public void testProcessInstance(){  
        //这个是专门管理流程实例的  
        ExecutionService executionService=engine.getExecutionService();  
        //通过流程定义文件的名称当做KEY来获取流程实例；也就是发布新流程  
        ProcessInstance processInstance=executionService.startProcessInstanceByKey("hello");  
        System.out.println("************"+processInstance);  
          
        //这里输出流程实例是否结束，针对本实例来说肯定是FALSE，因为本实例中有STATE，所以会在STATE这里处理等待状态，不会结束   
        //也就是接下来要说的执行等待的流程,除非你调用executionService.signalExecutionById  
        System.out.println(processInstance.isEnded());  
          
        //这个方法将是处理等待转该的流程执行到结束，返回值热仍然是一个流程实例  
        processInstance=executionService.signalExecutionById(processInstance.getId());  
        System.out.println(processInstance.isEnded());  
    }  
    public void testDeleteProcessInstance(){  
        ExecutionService executionService=engine.getExecutionService();  
        ProcessInstance processInstance=executionService.startProcessInstanceByKey("hello");  
        executionService.deleteProcessInstanceCascade(processInstance.getId());  
          
    }  
      
    public void testProcessInastanceList(){  
      ExecutionService executionService=engine.getExecutionService();  
      ProcessInstance pi=executionService.startProcessInstanceByKey("hello");  
      ProcessInstance pi1=executionService.startProcessInstanceByKey("hello");  
      List<ProcessInstance> pis=executionService.createProcessInstanceQuery().list();  
      for (ProcessInstance processInstance : pis) {  
        System.out.println(processInstance);  
    }  
    }  
}
